//
//  CangweiPickview.swift
//  AirTravel
//
//  Created by tulip on 2021/8/16.
//

import UIKit
import SwiftyJSON

class CangweiPickview: UIView ,UIPickerViewDelegate,UIPickerViewDataSource {
    
    
    var propType:(((Int,String))->Void)?
    var selectedType=(0,"仓位不限")
    var mbtn:UIButton!

    let  arr=["仓位不限","经济舱","头等舱/公务舱"]
    var picker:UIPickerView!
    var close:UIButton!
    var currentMapStr:String!
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.frame=CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        self.backgroundColor=UIColor.black.withAlphaComponent(0.7)
        let peopleview=UIView.init(frame: CGRect(x: 0, y: SCREEN_HEIGHT-300-BottomSpaceHeigh, width: SCREEN_WIDTH, height: 300))
        self.addSubview(peopleview)
        peopleview.backgroundColor=UIColor.white
        
        let title=UILabel(frame: CGRect(x: SCREEN_WIDTH/2.0-100, y:7, width: 200, height: 30))
        title.textColor=UIColor.init(hexString: "545454")
        title.font=UIFont.boldSystemFont(ofSize: 15)
        title.textAlignment = .center
        title.text="请选择仓位"
        peopleview.addSubview(title)
        
        
        let sure=UIButton.init(frame: CGRect(x: SCREEN_WIDTH-65, y: 7, width: 50, height: 30))
        sure.setTitle("确定", for: .normal)
        sure.setTitleColor(UIColor.init(hexString: "65a0ff"), for: .normal)
        sure.addTarget(self, action: #selector(makeSure), for: .touchUpInside)
        peopleview.addSubview(sure)
        
        let cancle=UIButton.init(frame: CGRect(x: 15, y: 7, width: 50, height: 30))
        cancle.setTitle("取消", for: .normal)
        cancle.setTitleColor(UIColor.init(hexString: "65a0ff"), for: .normal)
        cancle.addTarget(self, action: #selector(closeView), for: .touchUpInside)
        peopleview.addSubview(cancle)
        
        let line1=UIView.init(frame: CGRect(x: 0, y: 45, width: SCREEN_WIDTH, height: 1))
        line1.backgroundColor=UIColor.init(hexString: "efefef")
        peopleview.addSubview(line1)
       
        
        //时间 pick viwe
        picker=UIPickerView.init(frame: CGRect(x: 0, y: 60, width: SCREEN_WIDTH, height: 200))
        picker.delegate=self
        picker.dataSource=self
        picker.selectRow(0,inComponent:0,animated:true)
        peopleview.addSubview(picker)
        picker.backgroundColor=UIColor.white
        
        


        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if arr.count > 0{
            return arr.count
        }
        return 0
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if arr.count > 0{
            let titel = arr[row]
            return titel
        }
       return ""
        
        
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        50
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let xrow=pickerView.selectedRow(inComponent: 0)
        if arr.count > 0{
          selectedType=(xrow,arr[xrow])
          // print(xrow,arr[xrow])
        }
    }
    
    
    @objc func closeView(){
        self.removeFromSuperview()
    }
    
    
    @objc func makeSure(){
        self.removeFromSuperview()
        if (propType != nil) {
            self.propType!(selectedType)
        }
    }
  
    
}



